--
-- @Author:      name
-- @DateTime:    2018-08-9 23:05:48
-- @Description: 消息处理

local skynet = require "skynet"
local Network = require "Network"

local httpc = require("http.httpc")
local cjson = require("cjson")


local MessageHandler = class("MessageHandler")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function MessageHandler:ctor(message_dispatch)
	
	self.message_dispatch = message_dispatch	
	self.node_message = node_message
	local ip = "127.0.0.1"
	local port = 12201
	self.proto_file = skynet.getenv("proto_config") --proto文件路径
	self.network = Network.new(ip,port)

	self.user_id = nil
	self.banker_id = nil

	self:register()


	local host = "127.0.0.1:8001"
	local status, body = httpc.get(host, "./getGateReq", {client_type="socket"})	
	print("________http__", status, body)
	if status then 
		body = cjson.decode(body)
		print("___", body.ip)	
		--登录
		skynet.fork(function()
			skynet.sleep(200)
			-- self:userLoginReq()
			-- self:userRegister()
			-- self:userLoginAuthReq()

			self:touristRegisterReq()

		end)
	end
end

--注册本服务里的消息
function MessageHandler:register()

	self.message_dispatch:registerSelf('start',handler(self,self.start))

	self.network:register("userLoginAuthRes", handler(self,self.userLoginAuthRes))
	self.network:register("kickUserNt", handler(self,self.kickUserNt))
	
	self.network:register("enterHallRes", handler(self,self.enterHallRes))
	self.network:register("enterGameRes", handler(self,self.enterGameRes))
	self.network:register("enterRes", handler(self,self.enterRes))

	self.network:register("touristRegisterRes", handler(self, self.touristRegisterRes))



end

function MessageHandler:print(...)
	-- if self.user_id == 106 then 
		print(...)
	-- end
end

--游客注册
function MessageHandler:touristRegisterReq()
	local data = {
		seed_token = math.random(1000, 10000),
		package_channel = 'guangwang',
	}
	self.network:send("touristRegisterReq",data)
end








---------------------------------------------------------
-- cmd
---------------------------------------------------------

function MessageHandler:start(user_info)
	print("___________start__________")
	self.user_info = user_info
end


-- --用户登陆
-- function MessageHandler:userLoginReq()
-- -- //用户登
-- 	local data = {
-- 		version = "1.0",
-- 		mac_addr = "123",
-- 		device_type = 1,
-- 		channel_id = 1,
-- 		login_type = 2,
-- 		account = self.user_info.account,
-- 		password = self.user_info.password,
-- 		user_id = 1,
-- 		token = '',
-- 		mac_addr = "12324314",
-- 	}
-- 	self.network:send("userLoginReq",data)
-- end
-- --认证登陆
-- function MessageHandler:MSG_C2L_AuthLogin(data)

-- end




-------------------------------请求----------------------
--登陆认证
function MessageHandler:userLoginAuthReq(data)
	self.user_id = 121
	local data = {
		user_id = self.user_id,
		token = 'MTU1NjAwNzE0OQ==',
	}
	self:print("_____data__",data)
	self.network:send("userLoginAuthReq",data)
end
--登录成功
function MessageHandler:userLoginAuthRes(data)
	--token认证
	self:print("___token认证————",data, skynet.self())
	if data.error_code == 0 then 

		self:enterHallReq()
	end
end

-- function MessageHandler:userLoginRes(data)

-- 	print("___登录服务器登录结果____",data)
-- 	if not data.error_code or data.error_code == 0 then 
-- 		print("___登录服器成功通过___")
-- 		--切换登录网关
-- 		--用token去验证登录
-- 		self.network:close()
-- 		self.user_id =  data.user_info.user_id
-- 		local addr = data.server_addr
-- 		local ip,port = string.match(addr,"^([^:]*):(.*)$")
-- 		skynet.fork(function()
-- 			skynet.sleep(100)	
-- 			self.network:open(ip,port)
-- 			local data = {
-- 				user_id = data.user_info.user_id,
-- 				token = data.token,
-- 			}		
-- 			self:loginAuthReq(data)
-- 		end)
-- 	end	
-- end
--注册结果
function MessageHandler:userRegisterRes(data)
	if data.error_code and data.error_code > 0 then 
		self:print("______",data.error_msg)
		return
	end
	self:print("___注册成功____",data)
end

function MessageHandler:kickUserNt(data)
	print("_kickUserNt__",data)
	-- os.exit()
	skynet.exit()
end

--请求进入大厅
function MessageHandler:enterHallReq()
	local data = {
		user_id = self.user_id,
	}
	print("____enterHallReq",data)
	self.network:send("enterHallReq", data)
end
function MessageHandler:enterHallRes(data)
	self:print("____进入大厅返回",data)

	-- self:enterGameReq()	
	self.table_id = 1020101
	self:enterReq()	
end

--取桌子id
function MessageHandler:enterGameReq()
	local body = {
		user_id = self.user_id,
		room_id = 10201,
		-- table_id = nil,
	}
	self.network:send("enterGameReq",body)	
end
function MessageHandler:enterGameRes(data)
	self:print("____取游戏桌子id返回____________",data)
	self.table_id = data.table_id
	self:enterReq()
end

--进入游戏桌子
function MessageHandler:enterReq()
	local body = {
		table_id = self.table_id,
	}
	self.network:send("enterReq", body)	
end
function MessageHandler:enterRes(data)
	self:print("____进入桌子返回____________",data)
end


--------------游戏协议----------------------------------------------

------------------------req
--进入桌子
function MessageHandler:enterDeskReq(desk_id)
	local body = {
		desk_id = desk_id,
	}
	self.network:send("enterDeskReq",body)
end
function MessageHandler:enterDeskRes(data)
	self:print("_进入桌子__enterDeskRes_____",data)
	if data.error_code==0 then
		self.desk_id = data.desk_id
		self:deskStatusReq()
	end
end

function MessageHandler:leaveDeskReq(data)
	local body = {
		desk_id = 1,
	}
	self.network:send(self.cmd.DMSG_MDM_GAME,self.cmd.DMSG_C2G_LeaveDesk,body)
end
function MessageHandler:leaveDeskRes(data)
	self:print("_离开桌子_MSG_G2C_LeaveDeskResult______",data)
end

function MessageHandler:sitdownReq(data)
	local body = {
		desk_id = 1,
		seat_id = -1,
		desk_password = nil,
	}
	self.network:send("sitdownReq",body)
end
function MessageHandler:standupReq(data)
	local body = {
		desk_id = 1,
		seat_id = 1,
		force_leave	= nil,	
	}
	self.network:send("standupReq",body)
end

--请求桌子状态
function MessageHandler:deskStatusReq()
	local body = {
		desk_id = self.desk_id,			
	}
	self.network:send("deskStatusReq",body)	
end
function MessageHandler:deskStatusRes(data)
	print("____deskStatusRes__",data)
end



------------------------res

function MessageHandler:sitDownRes(data)
	self:print("__坐下__sitdownReqResult____",data)
end
function MessageHandler:standUpRes(data)
	self:print("__站起_standUpRes_____",data)
end

function MessageHandler:deskUserReq(data)
	print("______",data)
end
function MessageHandler:deskUserRes(data)
	print("___deskUserRes___",data)
end






--游客注册返回
function MessageHandler:touristRegisterRes(msg)
	print("____touristRegisterRes_", msg)
end


-------------------niuniu

return MessageHandler